import os
from numpy import info


def extract_major_from_filepath(file_path):
    """
    从完整的文件路径中提取专业名称
    例如 'C:/docs/学院/2024版统计学专业培养方案.docx' -> '统计学'
    """
    # 先从路径中取出文件名
    filename = os.path.basename(file_path)

    name = os.path.splitext(filename)[0]
    # 找到“专业培养方案”前的内容
    if "专业培养方案" in name:
        name = name.split("专业培养方案")[0]
    # 去掉“版”
    if "版" in name:
        name = name.split("版")[-1]
    elif "级" in name:
        name = name.split("级")[-1]
    
    # 去掉可能的前缀数字
    name = name.lstrip("0123456789")
    # 去掉前后空格
    name = name.strip()
    return name

def traverse_college_folder(root_folder):
    """
    遍历根目录下的所有学院文件夹，提取每个文件的专业信息及其学院
    返回一个字典，每个元素为{学院名: [专业名1, 专业名2, ...]}
    """
    def traversal(file_path):
        nonlocal current_college
        if os.path.isfile(file_path):
            major = extract_major_from_filepath(file_path)
            result[current_college].add(major)
        
        elif os.path.isdir(file_path):
            for path in os.listdir(file_path):
                traversal(os.path.join(file_path, path))


    result = dict()
    for current_college in os.listdir(root_folder):
        college_path = os.path.join(root_folder, current_college)
        # 初始化学院的专业集合
        if current_college not in result:
            result[current_college] = set()
        traversal(college_path)

    for k in result:
        result[k] = list(result[k])
    return result


if __name__ == "__main__":
    info = traverse_college_folder(input("培养方案目录"))
    print(info)